<?php

namespace app\admin\model;

use app\admin\common\Common;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\facade\Request;
use think\Model;

class ShopModel extends Model
{
    protected $pk = 'id';

    public static function addshop($data){
        try {
            $app = Db::name('app')->where('appid', $data['appid'])->find();
        } catch (DataNotFoundException $e) {
             return Common::result(400,$e->getMessage());
        } catch (ModelNotFoundException $e) {
             return Common::result(400,$e->getMessage());
        } catch (DbException $e) {
             return Common::result(400,$e->getMessage());
        }
        $adddata = [
            'shopname' => $data['shopname'],
            'shoptype' => $data['shoptype'],
            'money' => $data['money'],
            'vipnum' => $data['vipnum'],
            'inventory' => $data['inventory'],
            'appid' => $data['appid'],
            'creat_time' => date("Y-m-d H:i:s",time()),
            'shopimg' => $data['shopimg'],
        ];
        if ($app == null){
            return Common::result(400,'app不存在');
        }else{
            $user = Db::name('shop')->data($adddata)->insert();
            if ($user>0){
                return Common::result(200,'添加成功');
            }else{
                return Common::result(400,'添加失败');
            }
        }
    }

    public static function delshop($data){
        try {
            $shop = Db::name('shop')->delete($data['id']);
        } catch (PDOException $e) {
             return Common::result(400,$e->getMessage());
        } catch (Exception $e) {
             return Common::result(400,$e->getMessage());
        }
        if ($shop>0){
            return Common::result(200,"删除成功");
        }else{
            return Common::result(400,'删除失败');
        }
    }

    public static function delshoporder($data){
        try {
            $shop = Db::name('shoporder')->delete($data['id']);
        } catch (PDOException $e) {
             return Common::result(400,$e->getMessage());
        } catch (Exception $e) {
             return Common::result(400,$e->getMessage());
        }
        if ($shop>0){
            return Common::result(200,"删除成功");
        }else{
            return Common::result(400,'删除失败');
        }
    }



}